package yxy.game.pm2.command.fortdefeat;

import yxy.cherry.data.bean.runningdate;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Format.FormatType;
import yxy.game.pm2.bean.Hero;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.FortDefeatMessage;
import yxy.game.pm2.message.HeroMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.fortdefeat.FortDefeatHandle;
import yxy.game.pm2.module.fortdefeat.FortDefeatModule;
import yxy.game.pm2.module.fortdefeat.FortDefeatType;
import yxy.game.pm2.module.hero.FormatHandle;
import yxy.game.pm2.module.hero.HeroHandle;
import yxy.game.pm2.module.niubee.NiuBeeModule;

import java.util.Map;
import java.util.Map.Entry;

/** 副本过关斩将 过关斩将地图 */
@MessageType(proto = Command.FortDefeatMap, classtype = FortDefeatMessage.FortDefeatMapRequest.class)
public class CMD_FortDefeatMap extends Command<FortDefeatMessage.FortDefeatMapRequest> {

	@Override
	public void execute(MessageSource messageSource, FortDefeatMessage.FortDefeatMapRequest request) {
		String playerCode = messageSource.getTag();
		int typeId = request.getType();

		FortDefeatMessage.FortDefeatMapResponse.Builder response = FortDefeatMessage.FortDefeatMapResponse.newBuilder();

		FortDefeatModule defeatModule = FortDefeatModule.get(playerCode);

		FortDefeatType type = FortDefeatType.forNumber(typeId);
		if (type == null) {
			send(messageSource, response, Result.ParamError);
			return;
		}
		if (defeatModule.getType() != null) {
			type = defeatModule.getType();
		} else {
			switch (type) {
			case 困难:
				if (!defeatModule.getFinished().contains(FortDefeatType.普通)) {
					send(messageSource, response, Result.FortDefeatDifficultCantSelect);// 这个难度还不能挑战，请先完成上一个难度
					return;
				}
				break;
			case 炼狱:
				if (!defeatModule.getFinished().contains(FortDefeatType.困难)) {
					send(messageSource, response, Result.FortDefeatDifficultCantSelect);// 这个难度还不能挑战，请先完成上一个难度
					return;
				}
				break;
			default:
				break;
			}
			PlayerBean playerBean = PlayerBean.get(playerCode);
			runningdate data = new FortDefeatHandle().typeData(type);
			if (data != null) {
				if (playerBean.getPower() < data.getCondition()) {
					send(messageSource, response, Result.FortDefeatLowPower);// 战力不够
					return;
				}
			}

			try {
				if (defeatModule.tryLock()) {
					defeatModule.setFormat(new FormatHandle().get(playerCode, FormatType.普通));
					defeatModule.select(type);
				}
			} catch (Exception e) {
				send(messageSource, response, Result.DataError);
				return;
			} finally {
				defeatModule.unlock();
			}
		}

		// 基本信息
		response.setType(type.getNumber());// 地图类型
		response.setFinish(defeatModule.getMapId());// 完成进度
		for (Integer boxId : defeatModule.getBox()) {
			response.addBox(boxId);// 已领宝箱id
		}
		response.setSweep(defeatModule.getSweepId());// 可扫荡关卡
		response.addAllRewards(new CurrencyHandle().packItems(defeatModule.getHasRewards()));// 今日已获得的奖励
		response.setQuick(defeatModule.getAngers() != null);// 是否可快速挑战

		// 参展武将信息
		HeroHandle module = new HeroHandle();
		Map<String, Hero> heros = Hero.all(playerCode);
		for (Hero hero : heros.values()) {
			try {
				response.addHeros(module.getHeroSimple(playerCode, hero));// 武将信息 不带战力
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		FormatHandle formatHandle = new FormatHandle();

		response.setFormat(formatHandle.get(defeatModule.getFormat()));// 阵容
		// 神器信息
		NiuBeeModule niubeeModule = NiuBeeModule.get(playerCode);
		response.setNbAct(niubeeModule.getProgress());
		response.setNbLevel(niubeeModule.getSkillLevel());
		// 血量信息
		for (Entry<String, Integer> entry : defeatModule.getHeroHp().entrySet()) {
			String code = entry.getKey();
			int hp = entry.getValue();
			Hero hero = Hero.get(playerCode, code);
			if (hero != null) {
				HeroMessage.HeroHp.Builder builder = HeroMessage.HeroHp.newBuilder();
				builder.setCode(code);
				builder.setHp(hp);
				response.addHps(builder);
			}
		}
		response.setRevived(defeatModule.getReviveTimes());

		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, FortDefeatMessage.FortDefeatMapResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}

}